﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AionCxxLibrary.Enums;
using AionCxxLibrary.Utilities;
using WorldServer.Data;
using WorldServer.Packets.Server;

namespace WorldServer.Models
{
    public class StatsList : Dictionary<Stat, float>
    {
        AionObject obj;

        Dictionary<Stat, float> InitialStats = new Dictionary<Stat, float>();

        int Health;
        int Mana;
        ushort DivinePoints;
        ushort FlyPoints;

        public StatsList(AionObject obj)
        {
            this.obj = obj;

            InitializeStats();

            // Initialize Health and Mana
            Health = 5000;
            Mana = 10000;

            FlyPoints = (ushort)((obj is Character) ? 60 : 0);
            DivinePoints = 0;
        }

        public void ApplyEquipmentBonus(List<Item> equipment)
        {
            foreach (Item item in equipment)
            {
                AddBonus(item, false);
            }
        }

        public void AddBonus(Item item, bool RefreshStats)
        {
            lock (this)
            {
                foreach (KeyValuePair<Stat, StatEntry> stat in item.Template.Modifiers)
                {
                    Log.Debug("Stat: " + stat.Key.ToString());
                    float currentStat = this[stat.Key];
                    float newStat = stat.Value.Apply(currentStat, true);
                    UpdateStat(stat.Key, newStat);
                    Log.Debug("StatUpdate " + obj.GetGUID() + " " + stat.ToString() + " " + currentStat + " -> " + newStat + " when equipping " + item.Template.Name);
                }
                if (RefreshStats && obj is Character)
                {
                    (obj as Character).SendPacket(new SPkt_CharacterStats());
                }
            }
        }

        public void RemoveBonus(Item item, bool RefreshStats)
        {
            lock (this)
            {
                foreach (KeyValuePair<Stat, StatEntry> stat in item.Template.Modifiers)
                {
                    float currentStat = this[stat.Key];
                    float newStat = stat.Value.Apply(currentStat, false);
                    UpdateStat(stat.Key, newStat);
                    Log.Debug("StatUpdate " + obj.GetGUID() + " " + stat.ToString() + " " + currentStat + " -> " + newStat + " when unequipping " + item.Template.Name);
                }
                if (RefreshStats && obj is Character)
                {
                    (obj as Character).SendPacket(new SPkt_CharacterStats());
                }
            }
        }

        public void InitializeStats()
        {
            if (obj is Character)
            {
                Character c = obj as Character;
                float[] stats = CharacterBaseStats.Stats[(int)c.Template.Class];

                InitializeStat(Stat.MaxHp, 10000);
                InitializeStat(Stat.MaxMp, 10000);

                InitializeStat(Stat.Speed, 6);
                InitializeStat(Stat.FlySpeed, 9);
                InitializeStat(Stat.AttackDelay, 1.5f);
                InitializeStat(Stat.Attack, stats[0]);
                InitializeStat(Stat.PhysicalAttack, stats[0]);
                InitializeStat(Stat.MagicalAttack, stats[0]);
                InitializeStat(Stat.Health, stats[1]);
                InitializeStat(Stat.Agility, stats[2]);
                InitializeStat(Stat.Accuracy, stats[3]);
                InitializeStat(Stat.Knowledge, stats[4]);
                InitializeStat(Stat.Will, stats[5]);
                InitializeStat(Stat.Dodge, stats[6]);
                InitializeStat(Stat.Block, stats[6]);
                InitializeStat(Stat.Parry, stats[6]);
                InitializeStat(Stat.Concentration, 10);
                InitializeStat(Stat.MainHandAttack, stats[7]);
                InitializeStat(Stat.OffHandAttack, stats[8]);
                InitializeStat(Stat.MainHandAccuracy, stats[8]);
                InitializeStat(Stat.OffHandAccuracy, stats[8]);
                InitializeStat(Stat.MainHandCritical, stats[9]);
                InitializeStat(Stat.OffHandCritical, stats[9]);
                InitializeStat(Stat.MagicalAccuracy, stats[10]);
                InitializeStat(Stat.MagicalCritical, 50);
                InitializeStat(Stat.DamageReduce, 0);
                InitializeStat(Stat.AttackRange, 1.5f);
                InitializeStat(Stat.PhysicalDefend, 0);
                InitializeStat(Stat.MagicalResist, 0);
                InitializeStat(Stat.ResistanceWind, 0);
                InitializeStat(Stat.ResistanceFire, 0);
                InitializeStat(Stat.ResistanceWater, 0);
                InitializeStat(Stat.ResistanceEarth, 0);
                InitializeStat(Stat.ElementalResistanceLight, 0);
                InitializeStat(Stat.ElementalResistanceDark, 0);
                InitializeStat(Stat.BoostMagicalSkill, 0);
                InitializeStat(Stat.PvPAttackRatio, 0);
                InitializeStat(Stat.PvPDefendRatio, 0);
                InitializeStat(Stat.BoostCastingTime, 0);
                InitializeStat(Stat.BoostHate, 0);
                InitializeStat(Stat.BoostHeal, 0);
                InitializeStat(Stat.MaxFp, 60);
                InitializeStat(Stat.MaxDp, 4000);
                InitializeStat(Stat.NaturalHealingHp, c.Template.Level + 3);
                InitializeStat(Stat.NaturalHealingMp, c.Template.Level + 6);
                InitializeStat(Stat.NaturalHealingFp, 4); // 1 per second, tick every 4 sec for NaturalHealing
                InitializeStat(Stat.PhysicalCritical, 0);
                InitializeStat(Stat.PhysicalCriticalResist, 0);
                InitializeStat(Stat.MagicalCriticalResist, 0);
                InitializeStat(Stat.PhysicalCriticalDamageReduce, 0);
                InitializeStat(Stat.MagicalCriticalDamageReduce, 0);
                InitializeStat(Stat.PhysicalAccuracy, 0);
            }
            else
            {
                // GameObjects
                InitializeStat(Stat.Speed, 6);
                InitializeStat(Stat.FlySpeed, 9);
            }
        }

        public void InitializeStat(Stat stat, float value)
        {
            InitialStats.Add(stat, value);
            this.Add(stat, value);
        }

        public void UpdateStat(Stat stat, float value)
        {
            lock (this)
            {
                if (!this.ContainsKey(stat))
                    this.Add(stat, 0);
                this[stat] = value;
            }
        }

        public float GetStat(Stat stat)
        {
            lock (this)
            {
                if (!this.ContainsKey(stat))
                    return 0;
                return this[stat];
            }
        }

        public float GetInitialStat(Stat stat)
        {
            return InitialStats[stat];
        }

        public int GetHealth() { lock (this) { return Health; } }
        public int GetHealthPercent() { lock (this) { return (int)(100 * (Health / GetStat(Stat.MaxHp))); } }
        public int GetMana() { lock (this) { return Mana; } }

        public ushort GetDivinePoints() { lock (this) { return DivinePoints; } }
        public void SetDivinePoints(ushort value) { lock (this) { DivinePoints = value; } }
        public ushort GetFlyPoints() { lock (this) { return FlyPoints; } }
        public void SetFlyPoints(ushort value) { lock (this) { FlyPoints = value; } }

    }
}
